package com.bharatonjava.hospital.web;

import javax.validation.Valid;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.Errors;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.bharatonjava.hospital.domain.Address;
import com.bharatonjava.hospital.domain.Patient;
import com.bharatonjava.hospital.dto.PatientProfileDto;
import com.bharatonjava.hospital.service.patient.IPatientService;
import com.bharatonjava.hospital.service.patient.PatientService;
import com.bharatonjava.hospital.utils.Constants;
import com.bharatonjava.hospital.web.formbean.PatientFormBean;

@Controller
@RequestMapping(value = "/patient")
public class PatientController {

	private final Logger log = Logger.getLogger(PatientController.class);

	@Autowired
	private IPatientService patientService;

	
	public void setPatientService(PatientService patientService) {
		this.patientService = patientService;
	}

	@RequestMapping(value = "/register", method = RequestMethod.GET)
	public ModelAndView showPatientRegistrationForm() {
		log.info("PatientRegistrationController - showPatientRegistrationForm");
		ModelAndView mav = new ModelAndView();
		PatientFormBean patient = new PatientFormBean();
		mav.addObject("p", patient);
		mav.setViewName("createPatient");
		return mav;
	}

	@RequestMapping(value = "/register", method = RequestMethod.POST)
	public ModelAndView createPatient(@ModelAttribute("p") @Valid PatientFormBean formBean,
			BindingResult bindingResult, Errors errors) {
		log.info("PatientRegistrationController - createPatient");
		log.info("PatientFormBean: " + formBean);

		ModelAndView mav = new ModelAndView();

		if (bindingResult.hasErrors()) {
			log.info("inside bindingResult.hasErrors()..");
			mav.setViewName("createPatient");
			return mav;
		}

		Patient p = new Patient();
		p.setFirstName(formBean.getFirstName());
		p.setMiddleName(formBean.getMiddleName());
		p.setLastName(formBean.getLastName());
		p.setGender(formBean.getGender());
		p.setDob(formBean.getDob());
		
		Address a = new Address();
		a.setBuilding(formBean.getBuilding());
		a.setStreet(formBean.getStreet());
		a.setCity(formBean.getCity());
		a.setState(formBean.getState());
		a.setCountry(formBean.getCountry());
		
		int status = patientService.createPatient(p,a);
		if (status == 1) {
			mav.setViewName("homepage");
		}
		return mav;
	}

	
	@RequestMapping(value = "/profile")
	public ModelAndView showPatientProfile(@RequestParam(value="patientId") Long patientId){
		log.info("PatientController - showPatientProfile");
		ModelAndView mav = new ModelAndView();
		
		PatientProfileDto dto = patientService.getPatientProfile(patientId);
		log.info("---"+ dto);
		mav.addObject("dto", dto);
		
		mav.setViewName(Constants.PATIENT_PROFILE_VIEW);
		return mav;
	}

}
